<?php

namespace application\Module;

use library\Core\Db\CDbCriteria;
use application\Model\AdminModel;
use application\Comm\Request;

class Admin
{

    public function attribute()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'username' => '用户名',
            'status' => '状态',
            'create_time' => '创建时间',
        ];
    }

    /**
     * 用户列表数据
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getList($select = [], $offset = 0, $limit = 20)
    {
        $criteria = new CDbCriteria();
        //搜索
        $search = Request::postParam('search');
        if ($search != null) {
            if (isset($search['username']) && $search['username'] != '') $criteria->addEqCondition('username', $search['username']);
            if (isset($search['name']) && $search['name'] != '') $criteria->addEqCondition('name', $search['name']);
            if (isset($search['status']) && $search['status'] != '') $criteria->addEqCondition('status', $search['status']);
        }
        //分页
        $page['current'] = Request::postParam('pageCurrent') ? Request::postParam('pageCurrent') : 1;
        $page['size'] = Request::postParam('pageSize') ? Request::postParam('pageSize') : $limit;
        $offset = ($page['current'] - 1) * $page['size'];
        //排序
        $orderField = Request::postParam('orderField') ? Request::postParam('orderField') : 'id';
        $orderDirection = Request::postParam('orderDirection');


        $criteria->select($select);
        $criteria->addEqCondition('is_del', 0);
        if ($orderDirection == 'asc') {
            $criteria->addOrderAsc($orderField);
        } else {
            $criteria->addOrderDesc($orderField);
        }
        $adminModel = new AdminModel();
        $page['total'] = $adminModel->getCount($criteria);

        $criteria->setOffset($offset);
        $criteria->setLimit($page['size']);

        $data = $adminModel->getListForArray($criteria);
        return ['data' => $data, 'page' => $page];
    }

}
